package it.frusso.android;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;
import android.app.Activity;;

public class BaseActivity extends android.app.Activity implements it.frusso.android.notepad.Const  {
	
	private Toast toast = null;
	
	
	protected void showToastMessage(String msg) {
		if (null == toast) {
			toast = Toast.makeText(getBaseContext(), null, Toast.LENGTH_SHORT);
			toast.setGravity(Gravity.CENTER, 0, 0);
		} 
		toast.setText(msg);
		toast.show();
	}
	

	protected void showToastMessage(int msgID) {
		if (null == toast) {
			toast = Toast.makeText(getBaseContext(), null, Toast.LENGTH_SHORT);
			toast.setGravity(Gravity.CENTER, 0, 0);
		} 
		toast.setText(msgID);
		toast.show();
	}

	/**
	 * Recupera le preferences (interne) associate all'applicazione 
	 */
	@Deprecated
	protected SharedPreferences getSharedPreferences() {
		SharedPreferences sp = getSharedPreferences(APP_NAME, android.app.Activity.MODE_PRIVATE);
		debug("Getting sharedRefernces for application " + APP_NAME);
		return sp;
	}
	
	/**
	 * Restituisce le preferences associate alle activity di configurazione
	 * @return
	 */
	protected SharedPreferences getApplicationPreferences() {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
		return prefs;
	}
		
	
	protected void debug(String msg) {
		Log.d(LOG_TAG, msg);
	}
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        debug(getClass().getName() + " - Starting Activity - onCreate(...)");
	}
	
	@Override
	protected void onStart() {
		super.onResume();
		debug(getClass().getName() + " - Resume Activity - onStart(...)");
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		debug(getClass().getName() + " - Resume Activity - onResume(...)");
	}
	
	@Override
	protected void onRestart() {
		super.onRestart();
		debug(getClass().getName() + " - Restart Activity - onRestart(...)");
	}
	
	
	@Override
	protected void onPause() {
		super.onPause();
		debug(getClass().getName() + " - Pausing Activity - onPause(...)");
	}
	
	
//	protected void showDialogBox(String title, String msg, Drawable icon) {
//		AlertDialog.Builder ad = new AlertDialog.Builder(this);
//		ad.setTitle(title);
//		ad.setPositiveButton("OK", null);
//		if (icon == null)ad.setIcon(android.R.drawable.ic_dialog_alert);
//		else ad.setIcon(icon);
//		
//		ad.setMessage(msg);
//		ad.show();
//	}
	
	protected String getDateAsString(long time) {
		SimpleDateFormat sd = new SimpleDateFormat();
		return sd.format(new Date(time));
		
	}
	
	

	/**
	 * Sposta il focus su una nuova activity
	 */
	protected void moveToActivity(Class<? extends Activity> activityClass, Bundle b) {
		Intent intent = new Intent(this, activityClass);
		if (b != null) intent.putExtras(b);
		debug("Transition to " + activityClass.getName() + " Activity");
		if (b != null) {
			for (String key : b.keySet()) {
				debug(" +-- Extra: " + key + " := " + b.getString(key));
			}
		}
		startActivity(intent);
	}
	
	
	protected void moveToActivityForResult(Class<? extends Activity> activityClass, int requestCode, Bundle b) {
		Intent intent = new Intent(this, activityClass);
		if (b != null) intent.putExtras(b);
		debug("Transition to " + activityClass.getName() + " Activity");
		if (b != null) {
			for (String key : b.keySet()) {
				debug(" +-- Extra: " + key + " := " + b.getString(key));
			}
		}
		startActivityForResult(intent, requestCode);
	}
	
	
	public String getAndroidID(Context context) {
		String android_ID = android.provider.Settings.System.getString(
				this.getContentResolver(), 
				android.provider.Settings.System.ANDROID_ID); 
		return android_ID;
	}
	
	
}
